package work.chenbo.sdk.easy_excel.controlller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author ChenBo
 * @className SimpleReadExcelController
 * @date 2020/4/15
 */
@Controller
public class SimpleReadExcelController {


    @PostMapping("/upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException{

        final BlockingQueue<UserRegVo> queue = new LinkedBlockingQueue<>();

        new Thread(()->{
            try {
                UserRegVo take = queue.take();
                System.out.println(take);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        EasyExcel.read(file.getInputStream(), UserRegVo.class, new AnalysisEventListener<UserRegVo>() {
            @Override
            public void invoke(UserRegVo data, AnalysisContext context) {
                queue.add(data);
                System.out.println(context);
            }


            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                System.out.println(context);
            }
        }).sheet().doRead();
        return "";
    }
}
